var express = require('express');
var router = express.Router();
var User = require('../model/user')

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});
//查询所以用户
router.get('/addUsr', function(req, res, next) {
  User.findAll().then(function(result){
    console.log(JSON.stringify(result))
    res.json({
      status:'200',
      content:JSON.parse(JSON.stringify(result))
    })
  }).catch(function(ex){
    console.log(ex)
  })
});
//新增用户
router.get("/regest",async (req, res, next)=>{
  var user = {
    username:'test',
    password:'123456'
  }
  var result = await User.create(user)
  try {
    if(result){
      console.log(result)
      res.json({
        status:200
      })
    }
  } catch (error) {
    console.log(error)
  }
})
//修改用过密码
router.post('/userUpdata', async (req ,res ,next)=>{
 
  var result = await User.update({password:req.body.password},{where:{id:req.body.id}})
  try {
    if(result){
      // console.log(result)
      res.json({
        status:200,
        msg:'更新成功'
      })
    }
  } catch (error) {
    console.log(error)
  }
})
//删除用户
router.get('/delUser', (req ,res ,next)=>{
  User.destroy({where:{id:5}}).then(function(result){
    res.json({
      status:200,
      msg:result
    })
  }).catch(function(err){
    res.json({
      status:500,
      msg:err
    })
  })
 
})
//通过id查找单个用hu
router.get('/findUserByid',function(req ,res ,next){
  // console.log(req)
  var id= req.query.id
  User.findOne({where:{id:id}}).then(function(result){
    if(result){
      res.json({
        status:200,
        msg:result
      })
    }else{
      res.json({
        status:200,
        msg:'用户不存在'
      })
    }
    
  }).catch(function(err){
    res.json({
      status:500,
      msg:err
    })
  })
})

module.exports = router;
